package demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.common.Result;
import demo.entity.School;
import demo.entity.University;
import demo.mapper.SchoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/school")
public class SchoolController {
  @Resource
  SchoolMapper schoolMapper;

//  @PostMapping
//  public Result<?> save(@RequestBody School school){
//    schoolMapper.insert(Uni);
//    return Result.success();
//  }
  @GetMapping
  public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(defaultValue = "") String search1,
                            @RequestParam(defaultValue = "")String search2,
                            @RequestParam(defaultValue = "")String search3) {
    LambdaQueryWrapper<University> wrapper = Wrappers.<University>lambdaQuery();
    if (StrUtil.isNotBlank(search1)) {
      wrapper.like(University::getUnname, search1);
    }
    if (StrUtil.isNotBlank(search2)) {
      wrapper.like(University::getCity, search2);
    }
    if (StrUtil.isNotBlank(search3)) {
      wrapper.like(University::getUntype, search3);
    }

    Page<University> userPage = schoolMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    return Result.success(userPage);
  }

}
